<div class="wrap">
<h2>Event List</h2>

<h3><a href="<?php echo EM_URL ?>new-event.php">New Event &raquo;</a></h3>

<form name="searchform" action="" method="get" style="float: left; width: 16em; margin-right: 3em;">
  <fieldset>
  <legend><?php _e('Search Events') ?></legend>
  <input type="hidden" name="page" value="<?php echo $_GET['page'] ?>" />
  <input type="text" name="s" value="<?php echo $_GET['s'] ?>" size="17" />
  <input type="submit" name="submit" value="<?php _e('Search') ?>"  />
  </fieldset>
</form>
<table id="the-list-x" width="100%" cellpadding="3" cellspacing="3" style="clear: left;">
	<tr>
		<th>ID</th>
		<th>Event Name</th>
		<th>Event Start</th>
		<th>Listing Status</th>
		<th></th>
		<th></th>
		<th></th>
	</tr>
	<?php
	global $current_user;

	$sql = "SELECT * from $em_event_table";
	$where_clause = " where 1 = 1";
	if (!em_can_admin_events())
	{
		$where_clause .= " and manager_id = $current_user->id";
	}
	if (isset($_GET['s']))
	{
		$where_clause .= " and event_name like '%". $_GET['s'] . "%'";
	}
	$offset = $_GET['paged'] * 10;
	$sql .= $where_clause . " order by event_start desc limit $offset, 10";

	$events = $wpdb->get_results($sql);
	if ($events ) {
	foreach ($events as $event) {
	$class = ('alternate' == $class) ? '' : 'alternate';
	?>
	<tr class="<?php echo $class; ?>">
		<td><?php echo $event->id ?></td>
		<td><?php echo $event->event_name ?></td>
		<td><?php echo mysql2date("m/d/Y", $event->event_start) ?></td>
		<td><?php echo $event->listing_status ?></td>
		<td><a href="<?php echo EM_URL ?>edit-event.php&amp;event_id=<?php echo $event->id ?>" class="edit">edit</a></td>
		<td><a href="<?php echo EM_URL ?>edit-questions.php&amp;event_id=<?php echo $event->id ?>" class="edit">questions</a></td>
		<td><a href="<?php echo EM_URL ?>participant-list.php&amp;event_id=<?php echo $event->id ?>" class="edit">participants</a></td>
	</tr>
	<?php }} else { ?>
	<tr><td colspan="6">You don't have any events listed yet.</td></tr>
	<?php } ?>
</table>

<?php
	$event_count = $wpdb->get_var("select count(*) from $em_event_table" . $where_clause);
	$page_count = $event_count / 10;
	$paged = $_GET['paged'] + 1;
?>

<div class="navigation">
	<div class="alignleft">
	<?php if ($page_count > $paged) :
		//$paged += 1; ?>
		<a href="<?php echo EM_URL . 'event-list.php&paged=' . $paged ?>"><?php _e('&laquo; Previous Entries') ?></a>
	<?php endif; ?>
	</div>
	<div class="alignright">
	<?php if (1 < $paged) :
		$paged -= 2; ?>
		<a href="<?php echo EM_URL . 'event-list.php&paged=' . $paged ?>"><?php _e('Next Entries &raquo;') ?></a>
	<?php endif; ?>
	</div>
</div>
</div>